import {reqGetCategory1, reqGetCategory2, reqGetCategory3} from "@/api/category";

const state = {
    category1List:[],
    category2List:[],
    category3List:[],
    category1Id:null,
    category2Id:null,
    category3Id:null,
}
const mutations = {
    STATE_CATEGORY1LIST(state,payload){
      state.category1List = payload
    },
    STATE_CATEGORY2LIST(state,payload){
      state.category2List = payload
    },
    STATE_CATEGORY3LIST(state,payload){
      state.category3List = payload
    },
    category1IdChange(state,payload) {
      state.category1Id = payload
    },
    category2IdChange(state,payload) {
      state.category2Id = payload
    },
}
const actions = {
    async getCategory1({ commit}){
       try {
         const result =  await reqGetCategory1()
         commit('STATE_CATEGORY1LIST',result)
       }catch (e){
         return Promise.reject(e)
       }
    },
    async getCategory2({commit,state}){
        try {
          const result = await reqGetCategory2(state.category1Id)
          commit('STATE_CATEGORY2LIST',result)
        }catch (e){
          return Promise.reject(e)
        }
    },
    async getCategory3({ commit,state}){
        try {
          const result = await reqGetCategory3(state.category2Id)
          commit('STATE_CATEGORY3LIST',result)
        }catch (e){
          return Promise.reject(e)
        }
    }
}


export default {
  namespaced:true,
  state,
  mutations,
  actions
}


